<?php 

namespace common\bot;


use think\Cache;
class BotListService{

	public static $instance = null;
	const  BOT_LIST_PRE = 'bot_list_key_';
	private function __construct(){
	}
	public static function getInstance(){
		if(self::$instance == null){
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function setBot($botId,$robot,$expire=7200){
		$key = self::BOT_LIST_PRE . $botId;
		
		Cache::store('redis')->set($key, serialize($robot),$expire);
	}
	
	public function getBot($botId){
		$key = self::BOT_LIST_PRE . $botId;
		return unserialize(Cache::store('redis')->get($key));
	}
	
	public function initRobot($botId){
		$robot = BotListService::getInstance()->getBot($botId);
		if(!$robot){
			$robot = LoginService::getInstance()->dologin();
			$botId = $robot->getBotId();
		}
		$steam = $robot->getSteam();
		if($steam->isLoggedIn()){
			BotListService::getInstance()->setBot($botId, $robot);
		}else{
			return false;
		}
		return $robot;
	}
	
	public function getOneRobot(){
		$botId = 1;
		return $this->initRobot($botId);
	}
}	

?>